package Eyjafjalla.action;

import Eyjafjalla.modcore.EyjafjallaTag;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DrawCardAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.vfx.cardManip.ExhaustCardEffect;

import java.util.ArrayList;
import java.util.Iterator;

public class TimeGoneAction extends AbstractGameAction {
    public TimeGoneAction(){
        actionType = ActionType.CARD_MANIPULATION;
        startDuration = duration = Settings.ACTION_DUR_FAST;
    }

    @Override
    public void update() {
        //思考特效 todo
        if(duration == startDuration){
            AbstractPlayer p = AbstractDungeon.player;
            p.drawPile.clear();
            p.discardPile.clear();
            for(AbstractCard tmp:p.hand.group){
                AbstractDungeon.effectList.add(new ExhaustCardEffect(tmp));
            }
            p.hand.clear();
            p.exhaustPile.clear();
            CardGroup copy = new CardGroup(p.masterDeck, CardGroup.CardGroupType.DRAW_PILE);
            ArrayList<AbstractCard> placeOnTop = new ArrayList();
            copy.shuffle(CloudPatch.shuffleCopy.copy());
            float DRAW_PILE_X = (float)Settings.WIDTH * 0.04F;
            float DRAW_PILE_Y = 50.0F * Settings.scale;

            Iterator<AbstractCard> var4 = copy.group.iterator();

            while (true){
                AbstractCard c;
                while(var4.hasNext()) {
                    c = var4.next();
                    if (c.isInnate) {
                        placeOnTop.add(c);
                    } else if (!c.inBottleFlame && !c.inBottleLightning && !c.inBottleTornado) {
                        if(!c.hasTag(EyjafjallaTag.INNATE_EYJAFJALLA)){
                            //固有云层的牌会被移除
                            c.target_x = DRAW_PILE_X;
                            c.target_y = DRAW_PILE_Y;
                            c.current_x = DRAW_PILE_X;
                            c.current_y = DRAW_PILE_Y;
                            p.drawPile.addToTop(c);
                        }
                    } else {
                        placeOnTop.add(c);
                    }
                }

                var4 = placeOnTop.iterator();

                while(var4.hasNext()) {
                    c = var4.next();
                    p.drawPile.addToTop(c);
                }

                addToTop(new TimeGoneNextAction());

                if (placeOnTop.size() > AbstractDungeon.player.masterHandSize) {
                    addToTop(new DrawCardAction(AbstractDungeon.player, placeOnTop.size() - AbstractDungeon.player.masterHandSize));
                }

                addToTop(new DrawCardAction(AbstractDungeon.player,AbstractDungeon.player.masterHandSize));

                placeOnTop.clear();
                break;
            }
        }

        this.tickDuration();
    }
}
